//
//  ViewController.m
//  14-UICollectionView-test
//
//  Created by 王道林 on 16/8/2.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "CercleCollectionReusableView.h"

#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSArray *arrPic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _arrPic = @[@"1", @"2", @"3", @"4", @"5", @"6"];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.bounds.size.width - 40) / 2, 100);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(300, 10, 10, 10);
    //sectionInset改变了bounds
    
    
    layout.headerReferenceSize = CGSizeMake(100, 100);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    
#if 0
    //  在insert  top   加view
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -300, self.view.frame.size.width, 295)];
    view.backgroundColor = [UIColor blueColor];
    [collectionView addSubview:view];
    
#endif
    
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"wang"];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    // 自定义的头标题视图
    [collectionView registerClass:[CercleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"wangheader"];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 1) {
        return 3;
    }
    return _arrPic.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
#pragma mark 每个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    if (indexPath.section == 1) {
        return CGSizeMake(screenWidth / 3 - 30, 100);
    } else
    {
        return CGSizeMake((self.view.bounds.size.width - 40) / 2, 100);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(self.view.bounds.size.width, 400);
    }
    return CGSizeMake(100, 100);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"wang" forIndexPath:indexPath];
        cell.iamgeview.image = [UIImage imageNamed:_arrPic[indexPath.item]];
        
        return cell;
    }
    else
    {
        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"wang" forIndexPath:indexPath];
        cell.iamgeview.image = [UIImage imageNamed:@"2"];
        return cell;
    }
}

// 头部

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        CercleCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"wangheader" forIndexPath:indexPath];
        return view;

    } else
    {
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        view.backgroundColor = COLORAUTO;
        return view;
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
